package com.baizhi.controller;

import com.baizhi.entity.Hero;
import com.baizhi.service.HeroService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("hero")
public class HeroController {
    @Autowired
    private HeroService heroService;

    @RequestMapping("add")
    public Map add(Hero hero){
        Map map = new HashMap();
        try {
            heroService.insert(hero);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map remove(Integer id){
        Map map = new HashMap();
        try {
            heroService.delete(id);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Hero getOne(Integer id){
        Hero hero = heroService.selectById(id);
        System.out.println(hero);
        return hero;
    }
    @RequestMapping("update")
    public Map update(Hero hero){
        System.out.println(hero);
        Map map = new HashMap();
        try {
            heroService.update(hero);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("show")
    public Map show(Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Hero> pageInfo = heroService.select(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
}
